using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 教师假期管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("teacher.course")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TeacherVacationController : ControllerBase
    {
        private readonly ITeacherVacationService _teacherVacationService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="teacherVacationService">教师假期服务</param>
        public TeacherVacationController(ITeacherVacationService teacherVacationService)
        {
            _teacherVacationService = teacherVacationService;
        }

        /// <summary>
        /// 获取教师假期分页列表
        /// </summary>
        /// <param name="request">分页请求参数</param>
        /// <returns>教师假期分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<TeacherVacationDto>>> GetPagedList([FromBody] TeacherVacationPageRequest request)
        {
            var result = await _teacherVacationService.GetPagedListAsync(request);
            return ApiResponse<PagedResult<TeacherVacationDto>>.Ok(result);
        }

        /// <summary>
        /// 获取教师假期详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>教师假期详情</returns>
        [HttpPost]
        public async Task<ApiResponse<TeacherVacationDto>> GetById([FromBody] IdRequest request)
        {
            var vacation = await _teacherVacationService.GetByIdAsync(request.Id);
            return ApiResponse<TeacherVacationDto>.Ok(vacation);
        }

        /// <summary>
        /// 获取教师的假期列表
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>教师假期列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<TeacherVacationDto>>> GetByTeacherId([FromBody] IdRequest request)
        {
            var vacations = await _teacherVacationService.GetByTeacherIdAsync(request.Id);
            return ApiResponse<List<TeacherVacationDto>>.Ok(vacations);
        }

        /// <summary>
        /// 根据日期范围获取教师假期
        /// </summary>
        /// <param name="request">日期范围请求</param>
        /// <returns>教师假期列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<TeacherVacationDto>>> GetByDateRange([FromBody] TDateRangeRequest request)
        {
            var vacations = await _teacherVacationService.GetByDateRangeAsync(
                request.TeacherId,
                request.StartDate,
                request.EndDate);
            return ApiResponse<List<TeacherVacationDto>>.Ok(vacations);
        }

        /// <summary>
        /// 创建教师假期
        /// </summary>
        /// <param name="createDto">创建参数</param>
        /// <returns>创建成功的教师假期</returns>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<TeacherVacationDto>> Create([FromBody] CreateTeacherVacationDto createDto)
        {
            var vacation = await _teacherVacationService.CreateAsync(createDto);
            return ApiResponse<TeacherVacationDto>.Ok(vacation);
        }

        /// <summary>
        /// 更新教师假期
        /// </summary>
        /// <param name="updateDto">更新参数</param>
        /// <returns>更新后的教师假期</returns>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<TeacherVacationDto>> Update([FromBody] UpdateTeacherVacationDto updateDto)
        {
            var vacation = await _teacherVacationService.UpdateAsync(updateDto);
            return ApiResponse<TeacherVacationDto>.Ok(vacation);
        }

        /// <summary>
        /// 删除教师假期
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("teacher.course")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _teacherVacationService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 检查时间是否有冲突
        /// </summary>
        /// <param name="request">时间冲突检查请求</param>
        /// <returns>是否有冲突</returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> CheckTimeConflict([FromBody] TimeConflictRequest request)
        {
            var hasConflict = await _teacherVacationService.HasTimeConflictAsync(
                request.TeacherId,
                request.StartTime,
                request.EndTime,
                request.ExcludeId);
            return ApiResponse<bool>.Ok(hasConflict);
        }
    }

    /// <summary>
    /// 日期范围请求
    /// </summary>
    public class TDateRangeRequest
    {
        /// <summary>
        /// 教师ID
        /// </summary>
        public Guid TeacherId { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }
    }

    /// <summary>
    /// 时间冲突检查请求
    /// </summary>
    public class TimeConflictRequest
    {
        /// <summary>
        /// 教师ID
        /// </summary>
        public Guid TeacherId { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 排除的假期ID
        /// </summary>
        public Guid? ExcludeId { get; set; }
    }
} 